package com.bct.jxc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bct.jxc.common.Constants;
import com.bct.jxc.common.ErrorManager;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.User;

/**
 * 用户类Dao(管理员、普通操作员等)
 * 针对(t_user表进行CRUD)
 * @author ASUS
 *
 */
public class UserDao implements BaseDao<User>{
	SqlManager manager = null;
	
	public UserDao() {
		super();
		manager = SqlManager.createInstance();
		manager.connectDB();//换取数据库连接
	}

	/**
	 * 查询用户账号密码是否正确
	 * @param user
	 * @return
	 */
	public boolean loginCheck(User user) {
		boolean res = false;
		try {
			//编写sql语句
			String sql = "select * from t_user where login_name = ? and password=?";
			//封装参数
			Object[] params = {user.getLoginName(),user.getPassword()};
			//获取结果集
			ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
			while(rs.next()) {
				res = true;
			}
			//manager.closeDB();//关闭数据源之后会报错
		} catch (SQLException e) {
			ErrorManager.printError("UserDao loginCheck()", e);
		}
		return res;
	}
	/**
	 * 通过登录名查询操作者信息
	 * @param username
	 * @return
	 */
	public User getUser(String userName) {
		//编写sql语句
		String sql = "select * from t_user where login_name = ?";
		//封装参数
		Object[] params = new Object[]{userName};
		//获取结果集
		ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
		User user = null;
		try {
			if(rs != null) {
				while(rs.next()) {
					String loginName = rs.getString("login_name");
					String password = rs.getString("password");
					String name = rs.getString("username");
					String power = rs.getString("power");
					user = new User(loginName, password, name, power);
				}
			}
		} catch (SQLException e) {
			ErrorManager.printError("UserDao getUser()", e);
		}
		return user;
	}
	/**
	 * 添加用户
	 */
	@Override
	public boolean add(User u) {
		String sql = "insert into t_user values(?,?,?,?)";
		Object params[] = {u.getLoginName(),u.getPassword(),u.getUsername(),u.getPower()};
		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 查询操作者
	 */
	@Override
	public List<User> finds(User u) {
		List<User> list = null;
		Object[] params = null;
		String sql = null;
		if (u == null) {
			sql = "select * from t_user";
		}
		ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
		if (rs != null) {
			list = new ArrayList<User>();
			try {
				while (rs.next()) {
					User user = new User();
					user.setLoginName(rs.getString("login_name"));
					user.setPassword(rs.getString("password"));
					user.setUsername(rs.getString("username"));
					user.setPower(rs.getString("power"));
					list.add(user);
				}
			} catch (SQLException e) {
				ErrorManager.printError("UserDao finds()", e);
			}
		}
		return list;
	}
	/**
	 * 修改操作员
	 */
	@Override
	public boolean update(User t) {
		String sql = null;
		Object[] params = null;
		if (!StringUtil.isEmpty(t.getLoginName())&& !StringUtil.isEmpty(t.getPassword())) {
			sql = "update t_user set password=? where login_name=?";
			params = new Object[] { t.getPassword(), t.getLoginName() };
		} else {
			sql = "update t_user set username=?,power=? where login_name=?";
			params = new Object[] { t.getUsername(), t.getPower(), t.getLoginName() };
		}

		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}
	/**
	 * 删除用户
	 * @param loginName
	 * @return
	 */
	public boolean delete(String loginName) {
		String sql = "delete from t_user where login_name=?";
		Object params[] = {loginName};
		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}
	@Override
	public boolean update(String id, Integer spare) {
		// TODO Auto-generated method stub
		return false;
	}
}
